package com.sr.examination.net;

import com.tl.commonlibrary.network.BaseNet;
import com.tl.commonlibrary.network.NetManager;
import com.tl.commonlibrary.network.RequestCallBack;
import com.tl.commonlibrary.network.RequestListener;
import com.tl.commonlibrary.network.bean.base.BaseBean;
import com.tl.commonlibrary.ui.beans.Answer;
import com.tl.commonlibrary.ui.beans.CoinBean;
import com.tl.commonlibrary.ui.beans.CollectionBean;
import com.tl.commonlibrary.ui.beans.ExamBean;
import com.tl.commonlibrary.ui.beans.ExamGrade;
import com.tl.commonlibrary.ui.beans.ExerciseBean;
import com.tl.commonlibrary.ui.beans.ExerciseListBean;
import com.tl.commonlibrary.ui.beans.HistoryBean;
import com.tl.commonlibrary.ui.beans.HomeBean;
import com.tl.commonlibrary.ui.beans.NewsBean;
import com.tl.commonlibrary.ui.beans.PicUploadInfoBean;
import com.tl.commonlibrary.ui.beans.RechargeBean;
import com.tl.commonlibrary.ui.beans.RechargeHistoryBean;
import com.tl.commonlibrary.ui.beans.SubjectiveScoreBean;
import com.tl.commonlibrary.ui.beans.User;
import com.tl.commonlibrary.ui.beans.VerifyCodeBean;
import com.tl.commonlibrary.ui.beans.WrongBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;

/**
 * Created by jiangzhongshan on 18/2/5.
 */

public class Net extends BaseNet {
    public static final InterfaceService service = NetManager.get().create(InterfaceService.class);

    public static void getHome(int levelId, RequestListener<BaseBean<HomeBean>> listener) {
        HashMap<String, Object> param = new HashMap<>();
        if (levelId > 0) {
            param.put("levelId", levelId);
        }
        Call<BaseBean<HomeBean>> call = service.getHome(getQuery(param));
        call.enqueue(new RequestCallBack<BaseBean<HomeBean>>(listener));
    }

    public static void getVerifyCode(String mobile, RequestListener<BaseBean<VerifyCodeBean>> listener) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        Call<BaseBean<VerifyCodeBean>> call = service.getVerifyCode(getBody(map));
        call.enqueue(new RequestCallBack<BaseBean<VerifyCodeBean>>(listener));
    }

    public static void register(String mobile, String verifyCode, String sms, String password, RequestListener<BaseBean<User>> listener) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("verifyCode", verifyCode);
        map.put("sms", sms);
        map.put("password", password);
        Call<BaseBean<User>> call = service.register(getBody(map));
        call.enqueue(new RequestCallBack<BaseBean<User>>(listener));
    }

    public static void resetPassword(String mobile, String verifyCode, String sms, String password, RequestListener<BaseBean> listener) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("verifyCode", verifyCode);
        map.put("sms", sms);
        map.put("password", password);
        Call<BaseBean> call = service.resetPassword(getBody(map));
        call.enqueue(new RequestCallBack<BaseBean>(listener));
    }

    public static void login(String mobile, String password, RequestListener<BaseBean<User>> listener) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("password", password);
        Call<BaseBean<User>> call = service.login(getBody(map));
        call.enqueue(new RequestCallBack<BaseBean<User>>(listener));
    }

    public static void getUserInfo(RequestListener<BaseBean<User>> listener) {
        Call<BaseBean<User>> call = service.getUserInfo(getQuery());
        call.enqueue(new RequestCallBack<BaseBean<User>>(listener).foreground(false));
    }

    public static void postUserInfo(HashMap<String, Object> map, RequestListener<BaseBean> listener) {
        Call<BaseBean> call = service.postUserInfo(getBody(map));
        call.enqueue(new RequestCallBack<BaseBean>(listener));
    }

    public static void getExamGradeList(RequestListener<BaseBean<ArrayList<ExamGrade>>> listener) {
        Call<BaseBean<ArrayList<ExamGrade>>> call = service.getExamGradeList(getQuery());
        call.enqueue(new RequestCallBack<BaseBean<ArrayList<ExamGrade>>>(listener));
    }

    public static void setExamGrade(int levelId, RequestListener<BaseBean> listener) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("levelId", levelId);
        Call<BaseBean> call = service.setExamGrade(getBody(map));
        call.enqueue(new RequestCallBack<BaseBean>(listener));
    }

    public static void getExamList(int level, int classId, int pageNum, int pageSize, RequestListener<BaseBean<ArrayList<ExamBean>>> listener) {
        HashMap<String, Object> map = new HashMap<>();
        if (level >= 0) {
            map.put("levelId", level);
        }
        map.put("classId", classId);
        map.put("pageNum", String.valueOf(pageNum));
        map.put("pageSize", String.valueOf(pageSize));
        Call<BaseBean<ArrayList<ExamBean>>> call = service.getExamList(getQuery(map));
        call.enqueue(new RequestCallBack<BaseBean<ArrayList<ExamBean>>>(listener));
    }

    public static void getExamList(int level, int classId, int pageNum, RequestListener<BaseBean<ArrayList<ExamBean>>> listener) {
        getExamList(level, classId, pageNum, PAGE_SIZE, listener);
    }

    public static Call getExerciseList(int type, long exerciseId, RequestListener<BaseBean<ExerciseListBean>> listener) {
        return getExerciseList(type, exerciseId, -1, listener);
    }

    public static Call getExerciseList(int type, long exerciseId, int pUserSummaryId, RequestListener<BaseBean<ExerciseListBean>> listener) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("paperId", exerciseId);
        Call<BaseBean<ExerciseListBean>> call = null;
        if (type == ExerciseListBean.TYPE_TRAIL) {
            call = service.getExerciseTrailList(getQuery(map));
        } else if (type == ExerciseListBean.TYPE_ALL) {
            call = service.getExerciseList(getQuery(map));
        } else if (type == ExerciseListBean.TYPE_COLLECTION) {
            call = service.getExerciseCollectionList(getQuery(map));
        } else if (type == ExerciseListBean.TYPE_WRONG_ANSWER) {
            map.put("pUserSummaryId", pUserSummaryId);
            call = service.getExerciseWrongList(getQuery(map));
        }
        if (call != null) {
            call.enqueue(new RequestCallBack<BaseBean<ExerciseListBean>>(listener));
        }
        return call;
    }

    public static Call getRechargeList(RequestListener<BaseBean<List<RechargeBean>>> listener) {
        Call<BaseBean<List<RechargeBean>>> call = service.getRechargeList(getQuery());
        call.enqueue(new RequestCallBack<BaseBean<List<RechargeBean>>>(listener));
        return call;
    }

    /**
     * @param action   0为取消收藏，1为收藏
     * @param paperId
     * @param itemId
     * @param listener
     * @return
     */
    public static Call collection(int action, long paperId, long itemId, RequestListener<BaseBean> listener) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("action", action);
        map.put("paperId", paperId);
        map.put("itemId", itemId);
        Call<BaseBean> call = service.collection(getBody(map));
        call.enqueue(new RequestCallBack<BaseBean>(listener));
        return call;
    }

    public static Call getCollectionList(RequestListener<BaseBean<ArrayList<CollectionBean>>> listener) {
        Call<BaseBean<ArrayList<CollectionBean>>> call = service.getCollectionList(getQuery());
        call.enqueue(new RequestCallBack<BaseBean<ArrayList<CollectionBean>>>(listener));
        return call;
    }

    public static Call getWrongList(RequestListener<BaseBean<ArrayList<WrongBean>>> listener) {
        Call<BaseBean<ArrayList<WrongBean>>> call = service.getWrongList(getQuery());
        call.enqueue(new RequestCallBack<BaseBean<ArrayList<WrongBean>>>(listener));
        return call;
    }

    public static Call getHistoryList(int page, RequestListener<BaseBean<ArrayList<HistoryBean>>> listener) {
        HashMap<String, Object> params = new HashMap<>();
        params.put(KEY_PAGE_NUM, page);
        Call<BaseBean<ArrayList<HistoryBean>>> call = service.getHistoryList(getQuery(params));
        call.enqueue(new RequestCallBack<BaseBean<ArrayList<HistoryBean>>>(listener));
        return call;
    }

    public static Call getHistoryDetail(int pUserSummaryId, RequestListener<BaseBean<ExerciseListBean>> listener) {
        HashMap<String, Object> params = new HashMap<>();
        params.put("pUserSummaryId", pUserSummaryId);
        Call<BaseBean<ExerciseListBean>> call = service.getHistoryDetail(getQuery(params));
        call.enqueue(new RequestCallBack<BaseBean<ExerciseListBean>>(listener));
        return call;
    }

    public static Call getRechargeHistoryList(RequestListener<BaseBean<ArrayList<RechargeHistoryBean>>> listener) {
        Call<BaseBean<ArrayList<RechargeHistoryBean>>> call = service.getRechargeHistoryList(getQuery());
        call.enqueue(new RequestCallBack<BaseBean<ArrayList<RechargeHistoryBean>>>(listener));
        return call;
    }

    public static Call examPurchase(long paperId, RequestListener<BaseBean<CoinBean>> listener) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("paperId", paperId);
        Call<BaseBean<CoinBean>> call = service.examPurchase(getBody(map));
        call.enqueue(new RequestCallBack<BaseBean<CoinBean>>(listener));
        return call;
    }

    public static Call getHelp(RequestListener<BaseBean<NewsBean>> listener) {
        Call<BaseBean<NewsBean>> call = service.getHelp(getQuery());
        call.enqueue(new RequestCallBack<BaseBean<NewsBean>>(listener));
        return call;
    }

    public static Call getNewestList(int pageNum, RequestListener<BaseBean<ArrayList<NewsBean>>> listener) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("pageNum", pageNum);
        Call<BaseBean<ArrayList<NewsBean>>> call = service.getNewestList(getQuery(param));
        call.enqueue(new RequestCallBack<BaseBean<ArrayList<NewsBean>>>(listener));
        return call;
    }

    public static Call getNewsDetail(int newsId, RequestListener<BaseBean<NewsBean>> listener) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("newsId", newsId);
        Call<BaseBean<NewsBean>> call = service.getNewsDetal(getQuery(param));
        call.enqueue(new RequestCallBack<BaseBean<NewsBean>>(listener));
        return call;
    }

    public static Call submitExercise(long pUserSummaryId, ArrayList<Answer> answers, RequestListener<BaseBean<ExerciseListBean>> listener) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("pUserSummaryId", pUserSummaryId);
        param.put("itemsAnswers", answers);
        Call<BaseBean<ExerciseListBean>> call = service.submitExercise(getBody(param));
        call.enqueue(new RequestCallBack<BaseBean<ExerciseListBean>>(listener));
        return call;
    }

    public static Call feedback(String content, RequestListener<BaseBean> listener) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("content", content);
        Call<BaseBean> call = service.feedback(getBody(param));
        call.enqueue(new RequestCallBack<BaseBean>(listener));
        return call;
    }

    public static Call submitSubjectiveScore(long pUserSummaryId, long itemId, ArrayList<SubjectiveScoreBean> itemsScore, RequestListener<BaseBean> listener) {
        HashMap<String, Object> param = new HashMap<>();
        param.put("pUserSummaryId", pUserSummaryId);
        param.put("itemId", itemId);
        param.put("itemsScore", itemsScore);
        Call<BaseBean> call = service.submitSubjectiveScore(getBody(param));
        call.enqueue(new RequestCallBack<BaseBean>(listener));
        return call;
    }

    public static Call getUploadUrl(RequestListener<BaseBean<PicUploadInfoBean>> listener) {
        Call<BaseBean<PicUploadInfoBean>> call = service.getUploadUrl(getBody());
        call.enqueue(new RequestCallBack<BaseBean<PicUploadInfoBean>>(listener));
        return call;
    }

    public static Call logout(RequestListener<BaseBean> listener) {
        Call<BaseBean> call = service.logout(getBody());
        call.enqueue(new RequestCallBack<BaseBean>(listener));
        return call;
    }

}

